//
//  AuctionSoreDetailVC.m
//  MyNewProject
//
//  Created by 王春辉 on 2020/1/5.
//  Copyright © 2020年 sun. All rights reserved.
//

#import "AuctionSoreDetailVC.h"
#import "WalletDetailTableViewCell.h"

#pragma mark -- mark 数据相关
#import "AuctionScoreDetailModel.h"

@interface AuctionSoreDetailVC () <UITableViewDelegate, UITableViewDataSource> {
    int _page;
}

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSArray <AuctionScoreDetailDataModel *>*dataArray;
@end
static NSString *const identifier = @"wallet_tableView_cell_identifier";
@implementation AuctionSoreDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.gk_navTitle = @"积分明细";
    UIView *line = [[UIView alloc] init];
    [self.view addSubview:line];
    line.backgroundColor = SKColorWithHex(0xF2F2F2);
    [self.view addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.mas_offset(SKTopBar_Height);
        make.height.mas_equalTo(0.5);
    }];
    [self initContentViews];
    [self.view bringSubviewToFront:line];
    [self requestJifenDetailList];
}


- (void)initContentViews {
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.mas_offset(SKTopBar_Height);
        make.bottom.mas_offset(-SKBottomSafe_Height);
    }];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestJifenDetailList)];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(requestMoreJifenDetailList)];
}

#pragma mark -- mark 懒加载
- (UITableView *)tableView {
    if (!_tableView) {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.showsVerticalScrollIndicator = false;
        _tableView.showsHorizontalScrollIndicator = false;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [UIView new];
        _tableView.tableHeaderView = [UIView new];
        _tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
        [_tableView registerClass:[WalletDetailTableViewCell class] forCellReuseIdentifier:identifier];
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

#pragma mark -- mark UITableViewDelegate UITableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WalletDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    AuctionScoreDetailDataModel *model = self.dataArray[indexPath.row];
    cell.nameLabel.text = model.type;
    cell.dateLabel.text = model.time;
    cell.contentLabel.text = model.jifen;
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return (52.0 / 667) * self.view.height;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:true];
    
}


#pragma mark -- mark 获取网络数据
- (void)requestJifenDetailList {
    _page = 1;
    SKWeakSelf;
    [self requestJeifenDetailListWith:_page Complete:^(NSArray<AuctionScoreDetailDataModel *> *data) {
        weakSelf.dataArray = data;
        [weakSelf.tableView reloadData];
    }];
}

- (void)requestMoreJifenDetailList {
    if (_page == 0) {
        [self endRefreshing];
        return; }
    _page += 1;
    SKWeakSelf;
    [self requestJeifenDetailListWith:_page Complete:^(NSArray<AuctionScoreDetailDataModel *> *data) {
        NSMutableArray *mArr = [NSMutableArray arrayWithArray:weakSelf.dataArray];
        [mArr addObjectsFromArray:data];
        weakSelf.dataArray = mArr;
        [weakSelf.tableView reloadData];
    }];
}

- (void)requestJeifenDetailListWith:(int)page Complete:(void(^)(NSArray <AuctionScoreDetailDataModel *>*))complete {
    if (!(self.uid && self.loginKey)) {
        return; }
    NSDictionary *param = @{@"page":@(_page),
                            @"uid":self.uid,
                            @"loginKey":self.loginKey
                            };
    SKWeakSelf;
    [YQNetworking getWithUrl:@"Shop/getJifenLog" params:param reloginRefreshBlock:^(id response) {
        
    } successBlock:^(id response) {
        AuctionScoreDetailModel *model = [AuctionScoreDetailModel modelWithDictionary:response];
        if ([model isSuccess]) {
            if (complete) {
                complete(model.data);}
        }else {
            [weakSelf showMessage:model.msg];
        }
        [weakSelf endRefreshing];
    } failBlock:^(NSError *error) {
        [weakSelf endRefreshing];
        [weakSelf debugShowMessage:error.localizedDescription];
    }];
}

- (void)endRefreshing {
    if ([self.tableView.mj_header isRefreshing]) {
        [self.tableView.mj_header endRefreshing];
    }
    if ([self.tableView.mj_footer isRefreshing]) {
        [self.tableView.mj_footer endRefreshing];
    }
}
@end
